package com.easemob.chatuidemo.service;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;
import com.easemob.chatuidemo.domain.CellInfo;
import com.easemob.chatuidemo.utils.Gps;
import com.easemob.chatuidemo.utils.GpsUtilsTool;

import java.util.ArrayList;

/**
 * Created by apc_allen on 15/5/31.
 */
public class GpsService extends Service {
    ArrayList<CellInfo> cellIds = null;
    private Gps gps=null;
    private boolean threadDisable=false;
    private final static String TAG=GpsService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();


    gps = new Gps(GpsService.this);

    new Thread(new Runnable(){
        @Override
        public void run() {
            while (!threadDisable) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(gps!=null){ //当结束服务时gps为空
                    //获取经纬度
                    Location location=gps.getLocation();
                    //如果gps无法获取经纬度，改用基站定位获取
                    if(location==null){
                        Log.v(TAG, "gps location null");
                        //2.根据基站信息获取经纬度
                        try {
                            location = GpsUtilsTool.callGear(GpsService.this, cellIds);
                        } catch (Exception e) {
                            location=null;
                            e.printStackTrace();
                        }
                        if(location==null){
                            Log.v(TAG, "cell location null");
                        }
                    }

                    //发送广播
                    Intent intent=new Intent();
                    intent.putExtra("lat", location==null?"":location.getLatitude()+"");
                    intent.putExtra("lon", location==null?"":location.getLongitude()+"");
                    intent.setAction("com.ljq.activity.GpsService");
                    sendBroadcast(intent);
                }

            }
        }
    }).start();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onDestroy() {
        threadDisable=true;
        if(cellIds!=null&&cellIds.size()>0){
            cellIds=null;
        }
        if(gps!=null){
            gps.closeLocation();
            gps=null;
        }
        super.onDestroy();
    }
}
